import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

//引入接口函数
import { getShopData ,getGoodsData } from "@/api/goods";

import { IGood } from "@/interface/goods";

export default new Vuex.Store({
  state: {
    //店铺数据
    shopData: {},
    goodsData:[]
  },
  getters:{
    goodlist(state){
      console.log(state.goodsData);
      return state.goodsData.map((v:IGood)=>
        v.name
      )
    },
  },
  mutations: {
    //设置店铺数据
    SET_SHOP_DATA(state, shopData) {
      state.shopData = shopData
    },
    SET_GOODS_DATA(state, data) {
      state.goodsData = data
    },
  },
  actions: {
    //获取店铺数据
    get_shop_data({ commit }) {
      return new Promise(async (resolve, reject) => {
        //发送请求
        let res = await getShopData();

        //解构数据
        let { data } = res.data;

        //把数据存进vuex 调用mutations里面的同步方法
        commit('SET_SHOP_DATA', data)

        //如果外面用 就甩一份出去
        resolve(data)
      })


    },
    get_goods_data({commit}){
      return new Promise(async(reslove)=>{
        let res = await getGoodsData()
        commit("SET_GOODS_DATA",res.data.goodsList)
        reslove(res.data.goodsList)
      })
    }
  },
  modules: {
  }
})
